package com.dingwen.treasure.file.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dingwen.treasure.easyexcel.ExcelEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Excel导入监听获取数据存库
 *
 * @author dingwen
 * @date 2022/11/16
 */
public class ExcelEntityAnalysisEventListener extends AnalysisEventListener<ExcelEntity> {

    /**
     * 批量大小
     */
    private static final Integer BATCH_COUNT = 2000;

    /**
     * 导入数据
     */
    private final List<ExcelEntity> dos = new ArrayList(60);

    @Override
    public void invoke(ExcelEntity excelEntity, AnalysisContext context) {
        if (dos.size() >= BATCH_COUNT) {
            // 执行存库
            dos.clear();
        }
        dos.add(excelEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (dos.size() == 0) {
            return;
        }
        // 执行存库
    }
}
